package com.chenliang.provider.service.impl;


import com.chenliang.provider.service.AmqpService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @program: sendRabbitMqMsg
 * @description:
 * @author: 陈亮
 * @create: 2022-05-05 21:07
 */
@Service
public class AmqpServiceImpl implements AmqpService{


    @Resource
    private RabbitTemplate rabbitTemplate;

//    @Override
//    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//        String id = correlationData.getId();
//
//        if (ack) {
//            System.out.println("id是:" + id + "的消息已成功发送");
//        } else {
//            System.out.println("id是:" + id + "的消息发送失败,原因是:" + cause);
//        }
//    }


    @Override
    public void send(String exchange, String topic, Object message) {

        //rabbitTemplate.setConfirmCallback(this);

        String uuid = UUID.randomUUID().toString().replaceAll("(-)", "");

        CorrelationData correlationData = new CorrelationData(uuid);

        this.rabbitTemplate.convertAndSend(exchange, topic, message, correlationData);

    }

//    @Override
//    public void returnedMessage(ReturnedMessage returned) {
//        System.out.println(returned);
//    }
}
